package cn.vshare.controller;

import cn.vshare.entity.Footprint;
import cn.vshare.entity.User;
import cn.vshare.entity.Video;
import cn.vshare.service.FootprintService;
import cn.vshare.util.DateUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class FootprintController {
    @Resource
    private FootprintService footprintService;

    @RequestMapping("queryVideosIsFavorite")
    public PageInfo<Video> queryVideosIsFavorite(int pageNo, int pageSize, String uid) {
        Page<Video> page=footprintService.queryVideosIsFavorite(pageNo,pageSize,uid);
        PageInfo<Video> pageInfo=new PageInfo<>(page);
        return pageInfo;
    }


    /**
     * 获取个人观看视频历史足迹
     * 本方法仅针对登录用户
     * @param pageNo
     * @param pageSize
     * @param session
     * @return
     */

    @RequestMapping("/getFootPoint")
    public Page<Footprint> getFootPoint(int pageNo, int pageSize, HttpSession session){
        Page<Footprint> page=new Page<>();
        User user = (User) session.getAttribute("USER");
        if(user!=null){
            String uid=user.getId();
            Map<String,Object> cond=new HashMap<>();
            cond.put("uid",uid);
            cond.put("pageNo",pageNo);
            cond.put("pageSize",pageSize);
            page=footprintService.queryFootprintByUid(cond);
        }
        return page;
    }



    @RequestMapping("/queryIsFavoriter")
    public int queryIsFavoriter(HttpSession session, String vid){
        User user = (User) session.getAttribute("USER");

        /*20181013wyl*/
        System.out.println("20181013=====查询当前视频vid是否已经被收藏========="+vid);

        Footprint footprint=null;
        if(user!=null){
            footprint=footprintService.queryIsExistRecord(user.getId(),vid);
            if(footprint.getIsFavorite()==0){
                return 0;//未收藏
            }else{
                return 1;//已收藏
            }
        }else {
            return 0;//用户为空
        }
    }
    @RequestMapping("/addFavorite")
    public String addFavorite(HttpSession session, String vid , int isFavorite){
        User user = (User) session.getAttribute("USER");

        /*20181013wyl*/
        //1-收藏
        //0-未收藏/取消收藏
        System.out.println("20181013=====isFavorite========="+isFavorite);

        Footprint footprint=null;
        if(user!=null){
            footprint=footprintService.queryIsExistRecord(user.getId(),vid);
            footprint.setIsFavorite(isFavorite);
            footprint.setFavoriteDate(DateUtil.getCurrDate());
            System.out.println(JSON.toJSONString(footprint));
            footprintService.writeFootprint(footprint);
            return "success2AddisFavorite";
        }else {
            return "USER IS NULL";
        }

    }

}
